<?php
namespace app\common\service;

use app\common\service\Base;
use think\facade\Db;
use think\facade\Cache;
//快速买单
class Maidan extends Base {

    //买单收款
    public function maidan(){
        $sid = input('param.sid') ? input('param.sid') : 0;
        $userlevel = Db::name('user_level')->where('rank',$this->user['level'])->find();

        if(request()->isPost()){
            $post = input('post.');
            $money = floatval($post['money']);
            if($money <=0){
                return $this->json(['status'=>0,'msg'=>'支付金额必须大于0']);
            }
            $paymoney = $money;
            //会员折扣
            $disprice = 0;
            if($userlevel && $userlevel['discount']>0 && $userlevel['discount']<10){
                $disprice = $paymoney * (1 - $userlevel['discount'] * 0.1);
            }
            $paymoney = $paymoney - $disprice;
            //优惠券
            $couponmoney = 0;
            $couponrid = 0;
            if($post['couponrid'] > 0){

            }
            $paymoney = $paymoney - $couponmoney;
            if($paymoney < 0) $paymoney = 0;

            //$mendian = Db::name('mendian')->where('id',$post['mdid'])->find();
            if($sid > 0){
                $supply = Db::name('suppliers')->where('id',$sid)->find();
                $name = $supply['name'];
                $logo = $supply['logo'];
            }else{
                $name = tpCache('basic.store_name');
                $logo = tpCache('basic.store_logo');
            }
            //创建订单
            $order = [];
            $order['ordernum'] = date('ymdHis').aid.rand(1000,9999);
            $order['sid'] = $sid;
            $order['user_id'] = $this->user_id;
            $order['title'] = '付款给'.$name;
            $order['money'] = $money;
            $order['paymoney'] = $paymoney;
            $order['disprice'] = $disprice;
            $order['couponrid'] = $couponrid;
            $order['couponmoney'] = $couponmoney; //优惠券抵扣
            $order['w_time'] = time();
            $order['platform'] = platform;
            $order['status'] = 0;
            $orderid = Db::name('maidan_order')->insertGetId($order);

            return ['status'=>1,'order_id'=>$orderid,'data_tbl'=>'maidan_order'];
        }

        $userinfo = Db::name('users')->where('user_id',$this->user_id)->find();
        $wallet = Db::name('wallet')->where('user_id',$this->user_id)->find();
        $userinfo['money'] = $wallet['money'];
        if($userinfo['paypwd']==''){
            $userinfo['haspwd'] = 0;
        }else{
            $userinfo['haspwd'] = 1;
        }
        if($sid > 0){
            $supply = Db::name('suppliers')->where('id',$sid)->find();
            $name = $supply['name'];
            $logo = $supply['logo'];
        }else{
            $name = tpCache('basic.store_name');
            $logo = tpCache('basic.store_logo');
        }
        $rdata = [];
        $adlist = [];
        $rdata['adlist'] = $adlist;
        $rdata['userinfo'] = $userinfo;
        $rdata['couponList'] = [];
        $rdata['name'] = $name;
        $rdata['logo'] = $logo;
        $rdata['mdlist'] = [];
        //判断是否登录返回给前端，没登录的话前端静默注册
        $user_id = $this->user_id;
        $need_login = 0;
        if(!$user_id){
            $need_login = 1;
        }
        $rdata['need_login'] = $need_login;
        return $rdata;
    }

    public function maidanlog(){
        $pagenum = input('post.pagenum');
        $st = input('post.st');
        if(!$pagenum) $pagenum = 1;
        $pernum = 20;
        $where = [];
        $where[] = ['aid','=',aid];
        $where[] = ['mid','=',mid];
        $where[] = ['status','=',1];
        if(input('param.keyword')) $where[] = ['ordernum','like','%'.input('param.keyword').'%'];
        $datalist = Db::name('maidan_order')->field('*,from_unixtime(paytime)paytime')->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
        if(!$datalist) $datalist = [];
        if(request()->isPost()){
            return $this->json(['status'=>1,'data'=>$datalist]);
        }
        $count = Db::name('maidan_order')->where($where)->count();
        $rdata = [];
        $rdata['count'] = $count;
        $rdata['datalist'] = $datalist;
        $rdata['pernum'] = $pernum;
        $rdata['st'] = $st;
        return $this->json($rdata);
    }
    public function maidandetail(){
        $id = input('param.id/d');
        $detail = Db::name('maidan_order')->where('aid',aid)->where('mid',mid)->where('id',$id)->find();
        $detail['paytime'] = date('Y-m-d H:i:s',$detail['paytime']);
        if($detail['couponrid']){
            $couponrecord = Db::name('coupon_record')->where('aid',aid)->where('mid',mid)->where('id',$detail['couponrid'])->find();
        }else{
            $couponrecord = false;
        }
        if($detail['mdid']){
            $mendian = Db::name('mendian')->field('id,name')->where('aid',aid)->where('id',$detail['mdid'])->find();
        }else{
            $mendian = false;
        }
        $rdata = [];
        $rdata['detail'] = $detail;
        $rdata['couponrecord'] = $couponrecord;
        $rdata['mendian'] = $mendian;
        return $this->json($rdata);
    }
}
